﻿using AnlianCommon.API.Commands.Roles;
using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.RolePerms
{
    public class RolePermQueryHandler : IRequestHandler<RolePermQueryCommand, ResultPage<RolePermDto>>
    {
        private readonly IBaseRepository<RolePerm> _rolePermsRepository;
        private readonly IBaseRepository<Role> _rolesRepository;
        private readonly IBaseRepository<Permission> _permissionsRepository;
        private readonly IMapper _mapper;

        public RolePermQueryHandler(IBaseRepository<RolePerm> rolePermsRepository, IBaseRepository<Role> rolesRepository, IBaseRepository<Permission> permissionsRepository, IMapper mapper)
        {
            _rolePermsRepository = rolePermsRepository;
            _rolesRepository = rolesRepository;
            _permissionsRepository = permissionsRepository;
            _mapper = mapper;
        }


        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<RolePermDto>> Handle(RolePermQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<RolePermDto>() { Code=(int)CommonErrorCode.Search,TotalCount=0,PagedCount=0};
            try
            {
                var rolePerm = await (from a in await _rolesRepository.GetAll()
                                      join b in await _rolePermsRepository.GetAll() on a.RoleId equals b.RoleId
                                      join c in await _permissionsRepository.GetAll() on b.PermissionId equals c.PermissionId
                                      where b.IsDeleted ==false
                                      select new RolePermDto()
                                      {
                                          RoleId=a.RoleId,
                                          RoleName=a.RoleName,
                                          RolePermId = b.RolePermId,
                                          RolePermName=b.RolePermName,
                                          PermissionId=c.PermissionId,
                                          PermissionName=c.PermissionName,
                                      }).ToListAsync();
                if (request.PermissionId > 0)
                {
                    rolePerm = rolePerm.Where(c => c.PermissionId == request.PermissionId).ToList();
                }
                var list = _mapper.Map<List<RolePermDto>>(rolePerm);
                result.TotalCount = list.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                list = list .OrderBy(c => c.PermissionId).Skip((request.PageIndex-1)*request.PageSize).ToList();
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
